package com.benio.ycfdisplayer;

import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.benio.ycfdisplayer.displayer.DisplayListener;
import com.benio.ycfdisplayer.displayer.Options;
import com.benio.ycfdisplayer.displayer.SimpleDisplayListener;
import com.benio.ycfdisplayer.displayer.Target;
import com.benio.ycfdisplayer.displayer.YcfDisplayer;
import com.benio.ycfdisplayer.imageloader.TargetBuilder;
import com.benio.ycfdisplayer.imageloader.UILDisplayOptions;
import com.benio.ycfdisplayer.imageloader.UILExecutor;
import com.benio.ycfdisplayer.imageloader.UILInitializer;

public class UILExecutorActivity extends AppCompatActivity {
    private static final String TAG = "UILDisplayerActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        YcfDisplayer.initialize(this, new UILInitializer(), new UILExecutor(), new TargetBuilder());
        setContentView(R.layout.activity_uil_executor);

        String uri1 = "http://image.tianjimedia.com/uploadImages/2012/236/878HE0W687U6.jpg";
        String uri2 = "http://pic.5tu.cn/uploads/allimg/1503/301537344770.jpg";
        String uri3 = "http://pic.58pic.com/58pic/13/71/53/93h58PICGX2_1024.jpg";

        final ImageView imageView1 = (ImageView) findViewById(R.id.image1);
        final ImageView imageView2 = (ImageView) findViewById(R.id.image2);
        final ImageView imageView3 = (ImageView) findViewById(R.id.image3);

        Options options = new Options.Builder()
                .setFailureImage(new ColorDrawable(Color.RED))
                .setPlaceHolderImage(new ColorDrawable(Color.GREEN))
                .build();

        UILDisplayOptions uilDisplayOptions = new UILDisplayOptions.Builder()
                .setPlaceHolderImage(new ColorDrawable(Color.BLUE))
                .setFailureImage(new ColorDrawable(Color.RED))
                .delayBeforeLoading(500)
                .cacheInMemory(true)
                .cacheOnDisk(false)
                .build();
        DisplayListener displayListener = new SimpleDisplayListener() {
            @Override
            public void onStart(String imageUri, View view) {
                Log.d(TAG, "onStart() called with: imageUri = [" + imageUri + "], view = [" + view + "]");
            }

            @Override
            public void onSuccess(String imageUri, View view, Object loadedData) {
                Log.d(TAG, "onSuccess() called with: imageUri = [" + imageUri + "], view = [" + view + "], loadedData = [" + loadedData + "]");
            }

            @Override
            public void onFailure(String imageUri, View view, Throwable throwable) {
                Log.d(TAG, "onFailure() called with: imageUri = [" + imageUri + "], view = [" + view + "], throwable = [" + throwable + "]");
            }
        };
        Target target = new Target() {
            @Override
            public View getView() {
                return imageView2;
            }

            @Override
            public boolean setDrawable(Drawable drawable) {
                imageView2.setImageDrawable(drawable);
                return true;
            }

            @Override
            public boolean setBitmap(Bitmap bitmap) {
                imageView2.setImageBitmap(bitmap);
                return true;
            }
        };
        DisplayListener listener = new DisplayListener() {
            @Override
            public void onSuccess(String imageUri, View view, Object loadedData) {
                imageView3.setImageBitmap((Bitmap) loadedData);
            }

            @Override
            public void onFailure(String imageUri, View view, Throwable throwable) {

            }
        };
        YcfDisplayer.get().display(uri1).options(options).listener(displayListener).into(imageView1);
        YcfDisplayer.get().display(uri2).options(uilDisplayOptions).listener(displayListener).into(target);
        YcfDisplayer.get().display(uri3).options(uilDisplayOptions).listener(listener).into((Target) null);
    }
}
